<?php

class Admin_MyTestController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery-1.8.1.min.js');
        
    }

    public function indexAction()
    {
        // action body
        @set_time_limit(3600);
        if(function_exists('ini_set'))
        {
            ini_set('max_execution_time',3600);
            ini_set("memory_limit","256M");
        }
        $url= "http://auction.artron.net/BJBG-0254/PMH202451/PZ2009903/";
        $flag=true;
        $i=1;
        while($flag){
            $res=$this->getCurlHtml($url);
            $dom = new Zend_Dom_Query($res);
            $results = $dom->query('ul.imgList.specWorks.clearfix li h3 a');
            foreach ($results as $res){
                echo $i."|";
                $this->singlePageParser($res->getAttribute('href'));
                //var_dump($res->getAttribute('href'));
                echo"<br/>";
                $i++;
            }
            $netPageUrl=$dom->query('.page-next')->current()->getAttribute('href');
            if($netPageUrl!=$url){
                $url=$netPageUrl;
            }else{
                $flag=false;
            }
            echo $netPageUrl."<br/>";
        }
        //var_dump($results->getDocument());
        //$this->singlePageParser("http://auction.artron.net/paimai-art0012767652/");
    }
    
    public function artronConfirmAction(){
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
		$db = Zend_Db::factory($config->resources->db);
		$adapter = new Zend_Paginator_Adapter_DbSelect($db->select()->from('txy_artron')->where('status=0'));
		$paginator = new Zend_Paginator($adapter);
		$paginator->setCurrentPageNumber($this->_getParam('page'));
		$this->view->paginator = $paginator;
		
    }
    
    public function ajaxArtronConfirmAction(){
        $this->_helper->layout()->disableLayout();
        $id=$this->getRequest()->getParam('id');
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
		$db = Zend_Db::factory($config->resources->db);
		$preRes=$db->fetchRow("select * from txy_artron where id=".$id);
		$data['name']=$this->getRequest()->getParam('title');
        $data['url_spell']=$this->getRequest()->getParam('urlspell');
        $imgName=str_ireplace("-", "_", $data['url_spell']);
        $data['key_words']=$this->getRequest()->getParam('keywords');
        $data['owner']=$this->getRequest()->getParam('owner');
        $data['text_desc']="玲珑美玉——当代玉雕精品无底价拍卖会，2012年04月28日专场。".$preRes['title'];
        $img=new Txy_Util_CopyImg();
        $month=(string)date('Ym',time());
		$daytime=(string)date('dHi',time());
		$baseDir=APPLICATION_PATH."/../images/caijiage/".$month."/".$daytime;
		if(!is_dir($baseDir)){
		    mkdir($baseDir, 0775);
		}
        $imgPath=$baseDir."/".$imgName.'.jpg';
        if($img->curlImage($preRes['picurl'],$imgPath)){
            $data['img']=DOMAIN_URL."/images/caijiage/".$month."/".$daytime."/".$imgName.'.jpg';
            $imgIconPath=$baseDir."/".$imgName.'_icon.jpg';
            if(copy($imgPath, $imgIconPath)){
                $imgResize=new Txy_Util_ImgResize();
                $imgResize->load($imgIconPath);
                $imgResize->centerSquareResize('387');
                $data['icon']=DOMAIN_URL."/images/caijiage/".$month."/".$daytime."/".$imgName.'_icon.jpg';
                $imgResize->save($imgIconPath);
            }
            
        }
        $data['weight']=$preRes['weight'];
        $data['size']=$preRes['size'];
        $data['price']=$preRes['price'];
        $data['date']=$preRes['date'];
        $data['type']='当代玉雕精品无底价拍卖会';
        $data['link']=$preRes['url'];
        $dbCaiJiaGe=new Application_Model_DbTable_CaiJiaGe();
        $dbCaiJiaGe->addNew($data);
        $db->update('txy_artron', array('status'=>1),'id='.$id);
        echo "success";
    }
    
    private function getCurlHtml($url){
        //$url = "http://auction.artron.net/BJBG-0254/PMH202451/PZ2009903/";
        $ch = curl_init($url); //初始化
        curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串，而非直接输出
        curl_setopt($ch, CURLOPT_COOKIE,  "artronlogin=true;userid=MB00528841;pt_2_uid=MB00528841;pt_2_username=opium;pt_2_info=2acd62024e281459f3f6b8b983a33693;artron_67ae_saltkey=77JjHi48;artron_67ae_auth=f0672dgpQMXXt8%2FtNkPeoYaMKnELAx0c%2BtXJF09tPBDXQZ%2Fsk0PpxO7PwYKTY5dEIApf1gNsOV8gOs6mr73EJg06xMQ"); //存储cookies
        $res=curl_exec($ch);
        curl_close($ch);
        return $res;
    }
    
    private function singlePageParser($url){
        $res=$this->getCurlHtml($url);
        $dom = new Zend_Dom_Query($res);
        $title=$dom->query('div.title.clearfix h1 em')->current()->nodeValue;
        $data['title']=utf8_decode($title);
        //$data['title']=$title;
        $img=$dom->query("#bigPic")->current()->getAttribute('src');
        $data['url']=$url;
        $data['picurl']=$img;
        $tempSizes=$dom->query('.worksInfo table td.col em');
        foreach ($tempSizes as $temp){
            if($temp->nodeValue!='2012-04-28'){
                //$tempSizesStr=$temp->nodeValue;
                $tempSizesStr=utf8_decode($temp->nodeValue);
                $tempSizesArray=explode("；", $tempSizesStr);
                $size=array();
                $weight=array();
                foreach ($tempSizesArray as $singleSize){
                    if(stristr($singleSize, "重")){
                        $weight[]=substr($singleSize,3);
                    }else{
                        $size[]=$singleSize;
                    }
                }
            }
        }
        $itemSize=implode(',', $size);
        $itemWieght=implode(',', $weight);
        $data['size']=$itemSize;
        $data['weight']=$itemWieght;
        //$tempPriec=$dom->query("ul.knockDown li.fb")->current()->nodeValue;
        $tempPriec=utf8_decode($dom->query("ul.knockDown li.fb")->current()->nodeValue);
        $tempPriec=trim(str_ireplace("RMB", "", $tempPriec));
        $price=str_ireplace(",", "", $tempPriec);
        $data['price']=$price;
        $data['date']="2012-04-28";
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
		$db = Zend_Db::factory($config->resources->db);
        $db->insert('txy_artron', $data);
        var_dump($data['title']);
    }

}

